Vigenère Cipher

Module 01 / Lesson 06

Video Tutorial


The Concept of Polyalphabetic Shift

The Vigenère cipher is a method of encrypting alphabetic text by using a series of interwoven Caesar ciphers, based on the letters of a keyword. It employs a 26×26 table called the Tabula Recta or Vigenère Square.


Mathematical Logic:

If A=0, B=1, etc., the encryption of a message M with a key K is:

Encryption: Ci = (Mi + Ki mod L) mod 26
Decryption: Mi = (Ci - Ki mod L + 26) mod 26

Python Implementation

def vigenere_encrypt(plaintext, key):
    ciphertext = ""
    key = key.upper()
    key_index = 0
    
    for char in plaintext.upper():
        if char.isalpha():
            # Shift value from the key
            shift = ord(key[key_index % len(key)]) - ord('A')
            # Encrypt character
            encrypted_char = chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
            ciphertext += encrypted_char
            key_index += 1
        else:
            ciphertext += char
    return ciphertext

# Example:
msg = "HELLO"
key = "KEY"
print(vigenere_encrypt(msg, key)) # Output: RIJVS